'use strict';

const debug                                                 = require('debug')('ee-core:app:boot');
const path                                                  = require('path');
const {loadException}                                       = require('../exception');
const {electronApp}                                         = require('../electron/app');
const {getArgumentByName, getBundleDir, getElectronCodeDir} = require('../ps');
const {loadConfig}                                          = require('../config');
const {loadLog}                                             = require('../log');
const {app}                                                 = require('./application');
const {loadDir}                                             = require('./dir');

const {getEnvConfig} = require('ex-electron-egg-config')
const envs           = getEnvConfig()

class ElectronEgg {
    
    /**
     * 构造
     * @param {boolean} selfManagedData 是否自己管理 userData，true=将 userData 放在安装目录中
     */
    constructor(selfManagedData = false) {
        if (selfManagedData === true) {
            const exeDir        = electronApp.getPath('exe');
            const appDataDir    = path.join(path.dirname(exeDir), 'appData')
            const userDataDir   = path.join(path.dirname(exeDir), 'userData')
            const logsDir       = path.join(userDataDir, 'logs')
            const crashDumpsDir = path.join(userDataDir, 'Crashpad')
            electronApp.setPath('appData', appDataDir);
            electronApp.setPath('userData', userDataDir);
            electronApp.setPath('sessionData', userDataDir);
            electronApp.setPath('logs', logsDir);
            electronApp.setPath('crashDumps', crashDumpsDir);
        }
        
        const baseDir    = electronApp.getAppPath();
        const {env}      = process;
        const environmet = getArgumentByName('env') || envs.PROD;
        const debugging  = getArgumentByName('debuger') === 'true';
        
        // Debugging source code
        let electronDir = getBundleDir(baseDir);
        if (debugging) {
            electronDir = getElectronCodeDir(baseDir);
        }
        
        const options = {
            env        : environmet,
            baseDir,
            electronDir,
            appName    : electronApp.getName(),
            userHome   : electronApp.getPath('home'),
            appData    : electronApp.getPath('appData'),
            appUserData: electronApp.getPath('userData'),
            appVersion : electronApp.getVersion(),
            isPackaged : electronApp.isPackaged,
            execDir    : baseDir
        }
        
        // exec directory (exe dmg dep) for prod
        if (environmet === envs.PROD && options.isPackaged) {
            options.execDir = path.dirname(electronApp.getPath('exe'));
        }
        
        // normalize env
        env.EE_ENV           = environmet;
        env.EE_APP_NAME      = options.appName;
        env.EE_APP_VERSION   = options.appVersion;
        env.EE_BASE_DIR      = options.baseDir;
        env.EE_ELECTRON_DIR  = options.electronDir;
        env.EE_USER_HOME     = options.userHome;
        env.EE_APP_DATA      = options.appData;
        env.EE_APP_USER_DATA = options.appUserData;
        env.EE_EXEC_DIR      = options.execDir;
        env.EE_IS_PACKAGED   = options.isPackaged;
        env.EE_SOCKET_PORT   = null;
        env.EE_HTTP_PORT     = null;
        debug('[constructor] options:%j', options)
        
        this.init();
    }
    
    init() {
        // basic functions
        loadException();
        loadConfig();
        loadDir();
        loadLog();
    }
    
    register(eventName, handler) {
        return app.register(eventName, handler);
    }
    
    run() {
        app.run();
    }
}

module.exports = {
    ElectronEgg
};